<template>
	<view class="container">
		<!-- 位置信息 -->
		<view class="location-section">
			<view class="location-loca">
				<image class="safe-loca-icon" src="/static/home/safe-loca.png"></image>
				当前参考位置
			</view>
			<view class="location-title">{{ currentLocation }}</view>
		</view>

		<!-- 紧急联系人部分 -->
		<!-- 未添加 -->
		<view class="emergency-section" @click="navigateTo('/pages/home/safeCenter/addEmergencyContacts')" v-if="!emergencyContacts">
			<view class="emergency-contacts">
				<image class="contacts-icon" src="/static/home/safe-con.png"></image>
				紧急联系人
			</view>
			<view class="section-title">添加紧急联系人</view>
		</view>
		<!-- 已添加 -->
		<view class="emergency-section" v-if="emergencyContacts">
			<view class="emergency-contacts-edit-warps">
				<view class="emergency-contacts">
					<image class="contacts-icon" src="/static/home/safe-con.png"></image>
					紧急联系人
				</view>
				<view class="emergency-contacts" @click="navigateTo('/pages/home/safeCenter/addEmergencyContacts', emergencyContacts)">
					<image class="contacts-icon" src="/static/home/safe-edit.png"></image>
					编辑
				</view>
			</view>
			<view class="emergency-contacts-info">
				<view @click="callPolice(emergencyContacts.phone)">{{ emergencyContacts.phone }}</view>
				<view>{{ emergencyContacts.contact }}</view>
			</view>
		</view>
		<!-- 底部信息 -->
		<view class="safe-tips">
			<view class="warning-text">遇到危险，请向警方求助！</view>
			<view class="legal-notice">谎报警情可能被处以5日以上10日以下拘留</view>
			<view class="call-button" @click="callPolice(110)">呼叫110</view>
		</view>
	</view>
</template>

<script>
import config from '@/config';

export default {
	data() {
		return {
			emergencyContacts: {
				phone: '',
				contact: ''
			},
			currentLocation: '定位中...'
		};
	},
	onShow() {
		this.getEmergencyContact();
	},
	onLoad() {
		// 配置微信签名
		this.getWeixinSignature();
	},
	onUnload() {},
	methods: {
		// 调用接口获取微信签名
		async getWeixinSignature() {
			wx.ready(() => {
				this.getCurrentAddress();
			});
		},
		// 调用微信jssdk获取当前定位
		getCurrentAddress() {
			wx.getLocation({
				type: 'gcj02',
				isHighAccuracy: true, // 开启高精度定位
				highAccuracyExpireTime: 3000, // 高精度定位超时时间（毫秒）
				success: (res) => {
					const longitude = res.longitude;
					const latitude = res.latitude;
					//调用高德api进行反解析
					this.requestLocation(longitude, latitude);
				},
				fail: (res) => {
					this.currentLocation = '定位失败';
				},
				complete: (res) => {
					if (res.errMsg !== 'getLocation:ok') {
						this.currentLocation = '定位失败';
					}
				}
			});
		},
		// 发送请求坐标反解析
		requestLocation(longitude, latitude) {
			uni.request({
				url: 'https://restapi.amap.com/v3/geocode/regeo',
				method: 'GET',
				data: {
					key: config.mapKey,
					location: `${longitude},${latitude}`,
					output: 'JSON'
				},
				success: (res) => {
					if (res.data.infocode === '10000') {
						// 地址组成信息
						const addressComponent = res.data.regeocode.addressComponent;
						// 完整地址
						const address = res.data.regeocode.formatted_address;
						this.currentLocation = address;
					} else {
						uni.showToast({
							title: `定位失败：${res.errMsg}`,
							icon: 'none'
						});
						this.currentLocation = '定位失败';
					}
				},
				fail: (err) => {
					this.currentLocation = '定位失败';
				}
			});
		},
		async getEmergencyContact() {
			try {
				const data = await this.$http.get('/h5/user/emergencyContact');
				this.emergencyContacts = { ...data.data };
			} catch (error) {}
		},
		// 拨打电话
		callPolice(phoneNumber) {
			// if (!this.currentLocation.includes('定位失败') && !this.currentLocation.includes('定位中')) {
			// 	this.$http.post('/h5/user/addLocationLog');
			// }

			this.$http.post('/h5/user/emergencyContact', {
				contact: this.emergencyContacts.contact,
				phone: this.emergencyContacts.phone,
				address: this.currentLocation
			});

			this.$makePhoneCall(phoneNumber, {
				success: () => {},
				fail: (err) => {
					uni.showToast({ title: '拨号失败: ' + err.errMsg, icon: 'none' });
				}
			});
		},
		// 导航到指定页面
		navigateTo(url, data) {
			uni.navigateTo({
				url: `${url}?info=${encodeURIComponent(JSON.stringify(data))}`
			});
		}
	}
};
</script>

<style scoped>
.location-section {
	margin: 20rpx 26rpx 20rpx 26rpx;
	background-color: #fff;
	border-radius: 16rpx;
	padding: 30rpx 0 32rpx 20rpx;
}

.location-loca {
	font-family: Source Han Sans CN, Source Han Sans CN;
	font-weight: 400;
	font-size: 28rpx;
	color: #999999;
	line-height: 42rpx;
	padding-bottom: 38rpx;
}

.safe-loca-icon {
	width: 32rpx;
	height: 32rpx;
	margin-right: 12rpx;
}

.location-title {
	font-family: Source Han Sans CN, Source Han Sans CN;
	font-weight: 500;
	font-size: 40rpx;
	color: #0b1f20;
	line-height: 60rpx;
	letter-spacing: 1px;
	padding: 0 64rpx;
}

.emergency-section {
	background-color: #fff;
	border-radius: 8px;
	padding: 20px;
	margin: 20rpx 26rpx 20rpx 26rpx;
}

.emergency-contacts {
	font-family: Source Han Sans CN, Source Han Sans CN;
	font-weight: 400;
	font-size: 28rpx;
	color: #999999;
	line-height: 42rpx;
	padding-bottom: 38rpx;
	display: flex;
	align-items: center;
}

.contacts-icon {
	width: 32rpx;
	height: 32rpx;
	margin-right: 12rpx;
}

.section-title {
	font-family: Source Han Sans CN, Source Han Sans CN;
	font-weight: 500;
	font-size: 40rpx;
	color: #999999;
	line-height: 60rpx;
	letter-spacing: 1px;
	padding-left: 64rpx;
}

.emergency-contacts-edit-warps {
	display: flex;
	align-items: center;
	justify-content: space-between;
}

.emergency-contacts-info {
	display: flex;
	justify-content: space-between;
	font-family: Source Han Sans CN, Source Han Sans CN;
	font-weight: 500;
	font-size: 40rpx;
	color: #0b1f20;
	line-height: 60rpx;
	letter-spacing: 1px;
	padding: 0 44rpx;
}

.warning-text {
	font-family: Source Han Sans CN, Source Han Sans CN;
	font-weight: 500;
	font-size: 40rpx;
	color: #ff3f39;
	line-height: 60rpx;
	letter-spacing: 1px;
	text-align: center;
}

.legal-notice {
	font-family: Source Han Sans CN, Source Han Sans CN;
	font-weight: 400;
	font-size: 28rpx;
	color: #999999;
	line-height: 42rpx;
	text-align: center;
	padding: 20rpx 0 58rpx 0;
}

.call-button {
	background: linear-gradient(90deg, #ff3f39 0%, #ff7937 100%);
	border-radius: 16rpx;
	padding: 24rpx 0;
	margin: 0 26rpx;
	font-family: Source Han Sans CN, Source Han Sans CN;
	font-weight: 500;
	font-size: 36rpx;
	color: #ffffff;
	line-height: 54rpx;
	text-align: center;
}

.safe-tips {
	position: fixed;
	left: 0;
	bottom: 40rpx;
	text-align: center;
	width: 100%;
}
</style>